/**
 * 
 */
package com.freecsim.sas.core.services;

import org.apache.commons.configuration.ConversionException;
import org.apache.commons.configuration.PropertyConverter;
import org.apache.commons.lang.StringUtils;

import com.freecsim.sas.core.exception.InitializeException;
import com.freecsim.sas.core.exception.ServiceStartException;

/**
 * Service抽象类
 * @author wucj
 *
 */
public abstract class GenericService implements IService {
	
	
	/**
	 * 服务配置属性名称-服务启动依赖关系
	 */
	public static final String ATTRIBUTE_DEPENDON = "dependon";
	
	/**
	 * 服务配置对象
	 */
	private ServiceConfigure confige;
	
	/**
	 * 服务名称
	 */
	private String name;
	
	/**
	 * 服务启动顺序
	 */
	private int starton = -1;
	
	/**
	 * 服务状态
	 */
	private Status status = Status.STATUS_STOP;

	/*
	 * (non-Javadoc)
	 * @see com.freecsim.server.framework.core.services.IService#getName()
	 */
	public String getName() {
		return name;
	}

	/*
	 * (non-Javadoc)
	 * @see com.freecsim.server.framework.core.services.IService#init(com.freecsim.server.framework.core.services.ServiceConfigure)
	 */
	public void init(ServiceConfigure config) throws InitializeException {
		this.confige = config;
		ServiceContext context = config.getServiceContext();
		Object nameValue = context.getAttribute(ATTRIBUTE_NAME);
		if(nameValue == null || nameValue.toString().trim().equals("")){
			throw new InitializeException("属性"+ ATTRIBUTE_NAME + "没有找到或为null");
		}
		Object startonValue = context.getAttribute(ATTRIBUTE_STARTON);
		if(startonValue != null){
			try{
				starton = PropertyConverter.toInteger(startonValue);
			}catch(ConversionException ex){
				throw new InitializeException("属性"+ ATTRIBUTE_STARTON + " 配置异常.", ex);
			}
		}
		this.name = String.valueOf(nameValue).trim();
	}

	/*
	 * (non-Javadoc)
	 * @see com.freecsim.server.framework.core.services.IService#getServiceConfigure()
	 */
	public ServiceConfigure getServiceConfigure() {
		return confige;
	}

	/*
	 * (non-Javadoc)
	 * @see com.freecsim.server.framework.core.services.IService#start()
	 */
	public void start() throws ServiceStartException {
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.freecsim.server.framework.core.services.IService#shutdown()
	 */
	public void shutdown() {

	}

	/*
	 * (non-Javadoc)
	 * @see com.freecsim.server.framework.core.services.IService#getStatus()
	 */
	public Status getStatus() {
		return status;
	}

	/*
	 * (non-Javadoc)
	 * @see com.freecsim.server.framework.core.services.IService#setStatus(com.freecsim.server.framework.core.services.IService.Status)
	 */
	public void setStatus(Status status) {
		this.status = status;
	}

	/* (non-Javadoc)
	 * @see com.freecsim.server.framework.core.services.IService#getStartOn()
	 */
	public int getStartOn() {
		return starton;
	}
}
